using System;
using System.Collections.Generic;

namespace MT5Trade.Models.MarketData
{
    /// <summary>
    /// 市场行情数据点
    /// </summary>
    public class MarketTick
    {
        public DateTime Time { get; set; }
        public decimal Open { get; set; }
        public decimal High { get; set; }
        public decimal Low { get; set; }
        public decimal Close { get; set; }
        public decimal AdjustedClose { get; set; }
        public long Volume { get; set; }
        public string Symbol { get; set; }

        // 计算属性
        public decimal Change => Close - Open;
        public decimal ChangePercent => Open != 0 ? (Change / Open) * 100 : 0;
        public decimal Volatility { get; set; } // 日内波动率

        // 技术指标辅助
        public decimal GetTypicalPrice() => (High + Low + Close) / 3;
        public decimal GetRange() => High - Low;
    }

    /// <summary>
    /// 市场波动率数据
    /// </summary>
    public class MarketVolatility
    {
        public string Symbol { get; set; }
        public DateTime CalculatedAt { get; set; }
        public double Daily { get; set; }
        public double Weekly { get; set; }
        public double Monthly { get; set; }
        public double Annualized { get; set; }
        public VolatilityRegime Regime { get; set; }

        // 历史分位数
        public double Percentile { get; set; }
        public double VIXEquivalent { get; set; }
    }

    /// <summary>
    /// 波动率状态
    /// </summary>
    public enum VolatilityRegime
    {
        Low,      // < 10% 年化
        Normal,   // 10-20% 年化
        High,     // 20-40% 年化
        Extreme   // > 40% 年化
    }

    /// <summary>
    /// 缺口事件
    /// </summary>
    public class GapEvent
    {
        public DateTime Time { get; set; }
        public string Symbol { get; set; }
        public decimal GapSize { get; set; }
        public decimal GapPercentage { get; set; }
        public GapType Type { get; set; }
        public decimal PreviousClose { get; set; }
        public decimal CurrentOpen { get; set; }
        public string Description { get; set; }

        // 缺口填补信息
        public bool IsFilled { get; set; }
        public DateTime? FillDate { get; set; }
        public int BarsToFill { get; set; }
    }

    /// <summary>
    /// 敞口分析结果
    /// </summary>
    public class ExposureAnalysis
    {
        public DateTime AnalysisDate { get; set; }
        public decimal TotalNominalExposure { get; set; }
        public decimal TotalRiskAdjustedExposure { get; set; }
        public Dictionary<string, ExposureDetail> ExposureBySymbol { get; set; }
        public List<GapEvent> GapRisks { get; set; }
        public List<HedgeRecommendation> HedgeRecommendations { get; set; }
        public double RiskScore { get; set; } // 0-1, 越高风险越大

        // VaR指标
        public decimal VaR95 { get; set; }
        public decimal VaR99 { get; set; }
        public decimal CVaR95 { get; set; }
    }

    /// <summary>
    /// 敞口详情
    /// </summary>
    public class ExposureDetail
    {
        public string Symbol { get; set; }
        public decimal NominalExposure { get; set; }
        public decimal RiskAdjustedExposure { get; set; }
        public MarketVolatility Volatility { get; set; }
        public decimal CurrentPrice { get; set; }
        public double PositionSize { get; set; }
        public PositionType Direction { get; set; }

        // 风险指标
        public decimal MaxLoss { get; set; }
        public decimal StressTestLoss { get; set; }
        public double Beta { get; set; }
    }

    /// <summary>
    /// 对冲建议
    /// </summary>
    public class HedgeRecommendation
    {
        public string Symbol { get; set; }
        public string HedgeInstrument { get; set; }
        public double HedgeRatio { get; set; }
        public decimal RecommendedSize { get; set; }
        public string Rationale { get; set; }
        public double ExpectedEffectiveness { get; set; }
        public decimal EstimatedCost { get; set; }
    }

    /// <summary>
    /// 市场场景
    /// </summary>
    public class MarketScenario
    {
        public string Symbol { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public List<MarketTick> MarketData { get; set; }
        public List<GapEvent> IdentifiedGaps { get; set; }
        public MarketVolatility Volatility { get; set; }
        public Dictionary<string, double> CorrelationMatrix { get; set; }
        public MarketRegime MarketRegime { get; set; }

        // 统计信息
        public double AverageReturn { get; set; }
        public double Skewness { get; set; }
        public double Kurtosis { get; set; }
        public int TradingDays { get; set; }
    }

    /// <summary>
    /// 市场状态
    /// </summary>
    public enum MarketRegime
    {
        RangeBound,   // 区间震荡
        Trending,     // 趋势行情
        Volatile,     // 高波动
        Crisis        // 危机模式
    }

    /// <summary>
    /// 对冲执行结果
    /// </summary>
    public class HedgeExecutionResult
    {
        public DateTime ExecutionTime { get; set; }
        public GapHedgeData Gap { get; set; }
        public MarketAdaptiveHedgeParams HedgeParams { get; set; }
        public decimal ExecutedPrice { get; set; }
        public decimal Slippage { get; set; }
        public bool IsSuccessful { get; set; }
        public string ErrorMessage { get; set; }

        // 执行后评估
        public decimal RealizedPnL { get; set; }
        public double HedgeEffectiveness { get; set; }
        public decimal TransactionCost { get; set; }
    }

    /// <summary>
    /// 时间间隔
    /// </summary>
    public enum Interval
    {
        OneMinute,
        FiveMinutes,
        FifteenMinutes,
        ThirtyMinutes,
        Hourly,
        Daily,
        Weekly,
        Monthly
    }

    /// <summary>
    /// 持仓类型
    /// </summary>
    public enum PositionType
    {
        Long,
        Short,
        Neutral
    }

    /// <summary>
    /// 持仓信息
    /// </summary>
    public class Position
    {
        public string Symbol { get; set; }
        public double Volume { get; set; }
        public PositionType Type { get; set; }
        public decimal EntryPrice { get; set; }
        public DateTime OpenTime { get; set; }
        public decimal ContractSize { get; set; }
        public decimal UnrealizedPnL { get; set; }
        public decimal Margin { get; set; }
    }
}